//
//  ViewController.m
//  登陆界面
//
//  Created by student on 16/2/20.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic,weak) UIView *whiteView;

@property (nonatomic,weak) UIButton *loginBtn;

@property (nonatomic,weak) UITextField *accountTextField;

@property (nonatomic,weak) UITextField *passwordTextField;

@property (nonatomic,weak) UITextField *testcodeTextField;

@property (nonatomic,weak) UIButton *codeBtn;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.view setBackgroundColor:[UIColor darkGrayColor]];
    [self setUpUI];
}
-(void)setUpUI{
    self.whiteView = [self setUpViewWithFrame:CGRectMake(0,100, 414, 250)];
    self.loginBtn = [self setUpBtnWithFrame:CGRectMake(10, 450, 394, 40) WithText:@"登陆"];
    [self setUpLabelWithFrame:CGRectMake(10, 20, 90, 70) WithText:@"账户:"];
    [self setUpLabelWithFrame:CGRectMake(10, 90, 90, 70) WithText:@"登陆密码:"];
    [self setUpLabelWithFrame:CGRectMake(10, 160, 90, 70) WithText:@"验证码:"];
    self.accountTextField = [self setUpTextFieldWithFrame:CGRectMake(120, 20, 300, 70) WithText:@"手机号/会员名/邮箱"];
    self.passwordTextField = [self setUpTextFieldWithFrame:CGRectMake(120, 90, 300, 70) WithText:@"请输入密码"];
    [self.passwordTextField setSecureTextEntry:YES];
    self.testcodeTextField = [self setUpTextFieldWithFrame:CGRectMake(120, 160,180, 70) WithText:@"请输入验证码"];
    self.codeBtn = [self setUpBtnWithFrame:CGRectMake(300, 260, 110, 70) WithText:nil];
    NSString *str = [NSString stringWithFormat:@"%i%i%i%i",arc4random()%10,arc4random()%10,arc4random()%10,arc4random()%10];
    [self.codeBtn setTitle:str forState:UIControlStateNormal];
    [self.codeBtn setBackgroundColor:[UIColor yellowColor]];
    [self.codeBtn addTarget:self action:@selector(makeTestCode) forControlEvents:UIControlEventTouchUpInside];
    [self.loginBtn addTarget:self action:@selector(makeSureLogin) forControlEvents:UIControlEventTouchUpInside];
}
-(UIView *)setUpViewWithFrame:(CGRect)frame{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    [view setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:view];
    return view;
}

-(UIButton *)setUpBtnWithFrame:(CGRect)frame
                      WithText:(NSString *)text{
    UIButton *btn = [[UIButton alloc] initWithFrame:frame];
    [btn setTitle:text forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]
    ;
    [self.view addSubview:btn];
    return btn;
}

-(void)setUpLabelWithFrame:(CGRect)frame
                  WithText:(NSString *)text{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:text];
    [label setTextColor:[UIColor blackColor]];
    [label setTextAlignment:NSTextAlignmentRight];
    [self.whiteView addSubview:label];
}
//设置textField的方法
-(UITextField *)setUpTextFieldWithFrame:(CGRect)frame
                               WithText:(NSString *)text{
    UITextField *textField = [[UITextField alloc] initWithFrame:frame];
    [textField setBackgroundColor:[UIColor whiteColor]];
    [textField setPlaceholder:text];
    [self.whiteView addSubview:textField];
    return textField;
}
//验证码按钮的触发事件
-(void)makeTestCode{
    NSString *str = [NSString stringWithFormat:@"%i%i%i%i",arc4random()%10,arc4random()%10,arc4random()%10,arc4random()%10];
    [self.codeBtn setTitle:str forState:UIControlStateNormal];
}
//登陆按钮的触发事件
-(void)makeSureLogin{
    if ([self.accountTextField.text isEqualToString:@"admin"] && [self.passwordTextField.text isEqualToString:@"12345678"] && [self.testcodeTextField.text isEqualToString:self.codeBtn.titleLabel.text]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"登陆成功" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else if([self.accountTextField.text isEqualToString:@"admin"] == 0){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"账户输入有误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else if([self.passwordTextField.text isEqualToString:@"12345678"] == 0){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"密码输入有误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
    else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证码输入有误" message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:sureAction];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
//点击屏幕收回键盘
- (IBAction)hideKeyBoard:(id)sender {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
